
require("config")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")

local GameScene  = class("GameScene", function()
    return display.newScene("GameScene")
end)

local userData = CCUserDefault:sharedUserDefault()

local MYXXTEAKEY      = 'oyx'
local MYXXTEAKEY2     = 'czl'

function GameScene:ctor()

  local item = ui.newImageMenuItem({image='CloseNormal.png',imageSelected='CloseSelected.png',listener=onClicked})
  local menu = ui.newMenu({item}):addTo(self)
  menu:pos(display.width-40,display.height-40)
  menu:setZOrder(4)
  local touch = display.newLayer():addTo(self)
  touch:setTouchEnabled(true)
  touch:addNodeEventListener(cc.NODE_TOUCH_EVENT, onTouch)

  ---[[
  if not userData:getBoolForKey('isExisted') then
    CCLuaLog('not have userData!!!')
    CCLuaLog('create userData')
    userData:setBoolForKey('isExisted', true)
  else
    CCLuaLog('userData isExisted!!!')
  end
  --]]

  
  local userPath = CCUserDefault:getXMLFilePath()
  --[[
  备份UserDefault以便被篡改后还原
  ]]
  local backData = io.readfile(userPath)
  --最后一个字符如果是换行或者空格等其它转义符，会出错，需要处理
  --在末尾增加字符
  local tmpData = backData .. MYXXTEAKEY2
  --加密内容
  local mydata = crypto.encryptXXTEA(tmpData, MYXXTEAKEY2)
  backData = nil
  tmpData = nil
  --写入myBackFile文件
  io.writefile('myBackFile',mydata,'w+b')
  mydata = nil
  --解密文件
  local backData2 = io.readfile('myBackFile')
  local mydata2 = crypto.decryptXXTEA(backData2, MYXXTEAKEY2)
  tmpData = string.sub(mydata2,1,mydata2:len() - MYXXTEAKEY2:len())           --裁剪掉增加的字符串
  CCLuaLog(tmpData)
  io.writefile('myBackFile2.xml',tmpData,'w')


end

local array = {}
function onClicked(tag)
  CCLuaLog('... onClicked ...')
 
  if userData:getBoolForKey('b1',true) then
    CCLuaLog('... not have b1 ...')
    userData:setBoolForKey('b1', false)
  end
  if userData:getIntegerForKey('i1',0) == 0 then
    CCLuaLog('... not have i1 ...')
    userData:setIntegerForKey('i1', 100)
  end
  if userData:getStringForKey('s1','isExisted') == 'isExisted' then
    CCLuaLog('... not have s1 ...')
    userData:setStringForKey('s1', 'this is string key')
  end
  if userData:getStringForKey('s2','isExisted') == 'isExisted' then
    CCLuaLog('... not have s2 ...')
    userData:setStringForKey('s2', '100')
  end

  local v = userData:getIntegerForKey('i1')
  printf('i1 type of :%s, value=%d',type(v),v)
  v = userData:getIntegerForKey('s2')
  printf('s2 type of :%s, value=%d',type(v),v)
  v = userData:getStringForKey('s2')
  printf('s2 type of :%s, value=%s',type(v),v)
  v = userData:getStringForKey('b1')
  printf('s2 type of :%s, value=%s',type(v),v)

  ---[[
  local key = 'k1'
  local enKey = crypto.encryptXXTEA(key, MYXXTEAKEY)
  if userData:getStringForKey(key,'isExisted') == 'isExisted' then
    CCLuaLog('... not have enKey ...')
    local value = 'this is string key'
    local enValue = crypto.encryptXXTEA(value, MYXXTEAKEY)
    userData:setStringForKey(key, enValue)
  end
  --]]
  local myValue = userData:getStringForKey(key)
  local deValue = crypto.decryptXXTEA(myValue, MYXXTEAKEY)
  printf('deValue type of :%s, value=%s',type(deValue),deValue)

  ---[[
  key = 'k3'
  if userData:getStringForKey(key,'isExisted') == 'isExisted' then
    CCLuaLog('... not have enKey ...')
    value = 100
    enValue = crypto.encryptXXTEA(value, MYXXTEAKEY)
    userData:setStringForKey(key, enValue)
  end
  --]]
  myValue = userData:getStringForKey(key)
  deValue = crypto.decryptXXTEA(myValue, MYXXTEAKEY)
  printf('deValue type of :%s, value=%s',type(deValue),deValue)
  deValue = tonumber(deValue)
  printf('tonumber deValue type of :%s, value=%d',type(deValue),deValue)

  ---[[
  key = 'k4'
  if userData:getStringForKey(key,'isExisted') == 'isExisted' then
    CCLuaLog('... not have enKey ...')
    value = 3.1415
    enValue = crypto.encryptXXTEA(value, MYXXTEAKEY)
    userData:setStringForKey(key, enValue)
  end
  --]]
  myValue = userData:getStringForKey(key)
  deValue = crypto.decryptXXTEA(myValue, MYXXTEAKEY)
  printf('deValue type of :%s, value=%s',type(deValue),deValue)
  deValue = tonumber(deValue)
  printf('tonumber deValue type of :%s, value=%g',type(deValue),deValue)

  return false
end


function onTouch(event)
  if event.name == 'began' then
    CCLuaLog('... onTouch ...')
  elseif event.name == 'moved' then


  end

 
  return true
 
end


function GameScene:onEnter()
	CCLuaLog('GameScene:onEnter')																				--启用定时器监听
end


function GameScene:enterScene(sceneName, args, transitionType, time, more)
	 print('GameScene:enterScene..................')
end

function GameScene:onEnterFrame(dt)
  
end

game = {}

function game.startup()
    CCFileUtils:sharedFileUtils():addSearchPath("res/")
    print('startup..................')
    game.enterMainScene()
end

function game.exit()
    CCDirector:sharedDirector():endToLua()
    print('exit..........')
end

function game.enterMainScene()
	print('GameScene.new',type(GameScene.new))
    display.replaceScene(GameScene.new(), "fade", 0.6, display.COLOR_WHITE)
end
return game